package com.freewater.springboot.demo.spel.operator;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

public class Main {
    public static void main(String[] args) {
        ExpressionParser parser = new SpelExpressionParser();
        // evaluates to true
        Boolean trueValue = parser.parseExpression("2 == 2").getValue(Boolean.class);

        // evaluates to false
        Boolean falseValue = parser.parseExpression("2 < -5.0").getValue(Boolean.class);

        // evaluates to true
        trueValue = parser.parseExpression("'black' < 'block'").getValue(Boolean.class);

        // Be careful with primitive types, as they are immediately boxed up to their wrapper types. For example, 1 instanceof T(int) evaluates to false, while 1 instanceof T(Integer) evaluates to true, as expected.
        // evaluates to false
        falseValue = parser.parseExpression(
                "'xyz' instanceof T(Integer)").getValue(Boolean.class);

        // evaluates to true
        trueValue = parser.parseExpression(
                "'5.00' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);

        // evaluates to false
        falseValue = parser.parseExpression(
                "'5.0067' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);

        //
        EvaluationContext context = new StandardEvaluationContext();
        context.setVariable("leave_count", 3.5);
        context.setVariable("leave_unit", "天");

        Expression expression = parser.parseExpression("(#leave_count>3 && #leave_unit==\"天\") || (#leave_count > 24 && #leave_unit==\"小时\")");
        Boolean value = expression.getValue(context, Boolean.class);
        System.out.println(value);
    }
}
